package it.polimi.traveldream.web.beans.employee;

import it.polimi.traveldream.ejbclient.dtos.TravelPackageDTO;
import it.polimi.traveldream.ejbclient.eaos.TravelPackageServiceEAO;
import it.polimi.traveldream.utils.exceptions.JustInUseException;
import it.polimi.traveldream.utils.exceptions.NotSuchProductException;
import it.polimi.traveldream.web.beans.shared.FacesContextHandler;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name="detailPackage")
@RequestScoped
public class DetailPackageBean {
	@EJB
	private TravelPackageServiceEAO tService;
	@ManagedProperty(value="#{listTravelPackages.selectedTravelPackage}")
	private TravelPackageDTO travelPackage;
	
	public DetailPackageBean() {
	}
	
	public TravelPackageDTO getTravelPackage() {
		return travelPackage;
	}

	public void setTravelPackage(TravelPackageDTO travelPackage) {
		this.travelPackage = travelPackage;
	}

	public String deleteTravelPackage() {
		try {
			tService.deletePredefinedPackage(travelPackage.getId());
			return "index";
		} catch (NotSuchProductException e) {
			FacesContextHandler.produceFacesMessage("growl", FacesContext.getCurrentInstance(),
					FacesMessage.SEVERITY_ERROR, "The selected package cannot be reached.");
			return "detailPackage";
		} catch (JustInUseException e) {
			FacesContextHandler.produceFacesMessage("growl", FacesContext.getCurrentInstance(),
					FacesMessage.SEVERITY_ERROR, "You cannot delete an overpast package or one already participated.");
			return "detailPackage";
		}
	}
	
}
